
// pages/index/callService/callService.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    codeNum:120,
    bindHouse_num:'',
    index:0,
    intro:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;

    wx.request({
      url: app.globalData.baseUrl + '/api/house/house_num',
      data: {
        session_key: app.gs()
      },
      success(res) {
        let houses = res.data.data;
        // let num = res.data.data
        if (houses.num.is_scan == 0) {
          let index = 0;
          let hou = [];
          for (let i = 0; i < houses.house.length; i++) {
            hou[i] = houses.house[i].name
          }
          hou.unshift('选择房号');
          _this.setData({
            houses: res.data.data,
            hou: hou,
            num: res.data.data.num,
            houseIndex: '选择房号'
          })
        } else if (houses.num.is_scan == 1) {
          let houNum;
          let index = 0;
          let hou = [];
          for (let i = 0; i < houses.house.length; i++) {
            hou[i] = houses.house[i].name
          }
          hou.unshift('选择房号');
          if (res.data.data.num.is_scan == 1) {
            if (res.data.data.num.house == 'undefined') {
              houNum = hou[0]
            } else {
              houNum = res.data.data.num.house
            }

          } else {
            houNum = hou[0]
          }
          _this.setData({
            houses: res.data.data,
            hou: hou,
            num: res.data.data.num,
            houseIndex: res.data.data.num.house,
            houNum: houNum,
          })
        }
      }
    })
    app.wxrequest({
      url: app.globalData.baseUrl + '/api/serve/index',
      method: "GET",
      header: {
        "content-type": 'application/json'
      },
      data: {
        session_key: app.gs()
      },
      success: function (res) {
        console.log(res);
        wx.hideLoading()
        var items = [];
        for (var i = 0; i < res.data.data.length; i++) {
          items[i] = '../../../imgs/noCheck.png'
        }
        items[0] = '../../../imgs/checked.png'
        _this.setData({
          serveName: res.data.data,
          items: items,
          serve_id: res.data.data[0].id
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  // 制作选择勾选
  checked(e){
    console.log(e)
    var that = this;
    var index = e.currentTarget.dataset.index;
    var items = that.data.items;
    // console.log('items1',items)
    // // 单选
    for(var i = 0; i < items.length;i++){
      items[i] = '../../../imgs/noCheck.png'
    }
    items[index] = '../../../imgs/checked.png'
    // // // 多选
    // var change = items[index];
    // if (change =='../../../imgs/checked.png'){
    //   change ='../../../imgs/noCheck.png'
    // }else if (change == '../../../imgs/noCheck.png') {
    //   change = '../../../imgs/checked.png'
    // }
    // items[index] = change
    that.setData({
      serve_id:e.currentTarget.id,
      items:items
    })
  },
  inputIntro(e){
    this.setData({
      intro: e.detail.value
    })
  },
  bindHouse_num(e){
    this.setData({
      house_num: e.detail.value
    })
  },
  sendService(){
    var that = this;
    if (that.data.houNum =='选择房号'){
      wx.showToast({
        title: '请输入房号',
      })
      return
    }
    // if (that.data.intro == '') {
    //   // console.log('that.data.intro',that.data.intro)
    //   wx.showToast({
    //     title: '请输入备注',
    //   })
    //   return
    // }
    app.wxrequest({
      url: app.globalData.baseUrl +'/api/serve/serve_call',
      data:{
        serve_id:that.data.serve_id,
        intro:that.data.intro,
        house_num:that.data.houNum,
        session_key:app.gs()
      },
      success:function(res){
        wx.hideLoading()
        wx.showToast({
          title: '呼叫成功！',
        })
        console.log(res)
      }
    })
  },
  bindPickerChange: function (e) {
    let _this = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let index = e.detail.value;
    console.log(index)
    let ind = parseInt(index) - 1;
    let houseId, house_num;
    let houseIndex = _this.data.houseIndex;
    if (ind == -1) {
      houseId = -1,
        house_num = _this.data.hou[0]
      houseIndex = _this.data.hou[0]
    } else {
      houseId = _this.data.houses.house[ind].id;
      house_num = _this.data.houses.house[ind].name;
      houseIndex = _this.data.houses.house[ind].name
    }
    _this.setData({
      index: index,
      houseId: houseId,
      house_num: house_num,
      houseIndex: houseIndex,
      houNum: house_num
    })
  },
  saoma(e) {

    let _this = this;
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        let scan = res.path;
        let path = scan.split('?')[0];
        //      分配房号-index
        if (path == 'pages/index/index') {
          let house_num = scan.match(/scene=(\S*)/)[1];

          wx.request({
            url: app.globalData.baseUrl + '/api/house/band',

            data: {
              session_key: app.gs(),
              house_num: house_num
            },
            success(res) {
              console.log('success', res)


              if (res.data.message == 200) {
                wx.showToast({
                  title: '确认完成'
                })
              //  setTimeout(function(){
              //    wx.switchTab({
              //      url: '../../index/index'
              //    })
              //  },2000)



                wx.request({
                  url: app.globalData.baseUrl + '/api/house/house_num',
                  data: {
                    session_key: app.gs()
                  },
                  success(res) {
                    let houses = res.data.data;
                    // let num = res.data.data
                    if (houses.num.is_scan == 0) {
                      let index = 0;
                      let hou = [];
                      for (let i = 0; i < houses.house.length; i++) {
                        hou[i] = houses.house[i].name
                      }
                      hou.unshift('选择房号');
                      _this.setData({
                        houses: res.data.data,
                        hou: hou,
                        num: res.data.data.num,
                        houseIndex: '选择房号'
                      })
                    } else if (houses.num.is_scan == 1) {
                      let houNum;
                      let index = 0;
                      let hou = [];
                      for (let i = 0; i < houses.house.length; i++) {
                        hou[i] = houses.house[i].name
                      }
                      hou.unshift('选择房号');
                      if (res.data.data.num.is_scan == 1) {
                        if (res.data.data.num.house == 'undefined') {
                          houNum = hou[0]
                        } else {
                          houNum = res.data.data.num.house
                        }

                      } else {
                        houNum = hou[0]
                      }
                      _this.setData({
                        houses: res.data.data,
                        hou: hou,
                        num: res.data.data.num,
                        houseIndex: res.data.data.num.house,
                        houNum: houNum,
                      })
                    }
                  }
                })
              }
            }
          })
        }
      }
    })
  }
})